import utils from '../../core/utils';
import logger from '../../core/logger';
import { apiKeyInfo } from './apiKeyInfo';

export class apiProvider {
  public static readonly loginKey: apiKeyInfo = new apiKeyInfo("783bbb7264451d82", "2653583c8873dea268ab9386918b1d65");
  public static readonly androidKey: apiKeyInfo = new apiKeyInfo("1d8b6e7d45233436", "560c52ccd288fed045859ed18bffd973");

  public static getSign(value: string, key: apiKeyInfo): string {
    try {
      let sign = '';
      if (value.indexOf('?') > -1) {
        let index = value.indexOf("?", 4) + 1;
        let str = value.substring(index);
        let array = str.split('&').sort();
        for (let i = 0; i < array.length; i++) {
          if (i != 0)
            sign += '&';
          sign += array[i];
        }
        sign += key.secret;
      }
      else {
        if (value.startsWith('&'))
          value = value.substring(1);
        let array = value.split('&').sort();
        for (let i = 0; i < array.length; i++) {
          if (i != 0)
            sign += '&';
          sign += array[i];
        }
        sign += key.secret;
      }
      sign = utils.md5(sign).toString();
      return `&sign=${sign}`;
    } catch (err) {
      logger.error(err);
      return value;
    }
  }
}